package com.gnerv.sylvanas.framework.common.tools;

import java.io.File;

/**
 * 常用地址工具
 *
 * @author Gnerv LiGen
 */
public class PathTools {

    private static final String USER_DIR = "user.dir";
    private static final String CONFIG = "/config";
    private static final String LOGS = "/logs";
    private static final String SCRIPT = "/bin";
    private static final String COLLECTOR = "/collector";
    private static final String TEMPLATES = "/templates";

    private PathTools() {
    }

    /**
     * 项目运行根目录的绝对地址
     */
    public static String projectDirectory() {
        return System.getProperty(USER_DIR);
    }

    /**
     * 项目运行根目录下的子目录的绝对地址
     */
    public static String projectDirectory(String name) {
        return getDirectoryPath(name);
    }

    /**
     * 项目运行配置文件目录的绝对地址
     */
    public static String projectConfigDirectory() {
        return getDirectoryPath(CONFIG);
    }

    /**
     * 项目运行日志文件目录的绝对地址
     */
    public static String projectLogDirectory() {
        return getDirectoryPath(LOGS);
    }

    /**
     * 项目运行脚本文件目录的绝对地址
     */
    public static String projectScriptDirectory() {
        return getDirectoryPath(SCRIPT);
    }

    public static String projectCollectorDirectory() {
        return getDirectoryPath(COLLECTOR);
    }

    public static String projectTemplatesDirectory() {
        return getDirectoryPath(TEMPLATES);
    }

    private static String getDirectoryPath(String name) {
        File directory = getDirectory(name);
        return directory != null ? directory.getAbsolutePath() : null;
    }

    private static File getDirectory(String name) {
        File file = new File(System.getProperty(USER_DIR), name);
        boolean exists = file.exists();
        if (exists) {
            boolean directory = file.isDirectory();
            if (directory) {
                return file;
            }
        }
        return null;
    }
}
